package com.shaoshaohuo.app.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.CouponAdapter;
import com.shaoshaohuo.app.entity.MyCouponEntity;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.utils.Okhttputils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 */
public class CouponFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;

    private View view;
    private ListView coupon_listView;
    private List<MyCouponEntity.DataBean> coupon_listView_data;
    private CouponAdapter coupon_listView_Adapter;
    private RelativeLayout coupon_no_data_relativeLayout;

    public CouponFragment() {
        // Required empty public constructor
    }

    // TODO: Rename and change types and number of parameters
    public static CouponFragment newInstance(String param1, String param2) {
        CouponFragment fragment = new CouponFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString("coupon");
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_coupon, container, false);
        Log.e("TAG", "onCreateView: " + mParam1);
        initView();
        loadData();
        bindAdapter();
        return view;
    }

    private void loadData() {
        //请求数据
        ShopHttpConfig.obtainCoupon(getActivity(), mParam1, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                MyCouponEntity myCouponEntity = (MyCouponEntity) o;
                coupon_listView_data = myCouponEntity.getData();
                if (coupon_listView_data.size() == 0) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            coupon_no_data_relativeLayout.setVisibility(View.VISIBLE);
                            coupon_listView.setVisibility(View.GONE);
                            Log.e("TAG", "run: " + coupon_listView_data.size());
                        }
                    });

                }
                if (coupon_listView_data.size() != 0) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            coupon_no_data_relativeLayout.setVisibility(View.GONE);
                            coupon_listView.setVisibility(View.VISIBLE);
                            coupon_listView_Adapter.refreshData(coupon_listView_data);
                            Log.e("TAGAAA", "run: " + coupon_listView_data.size() + mParam1);
                        }
                    });
                }

            }
        }, MyCouponEntity.class);
//        ShopHttpConfig.obtainCoupon(getActivity(), new Okhttputils.OkutListener() {
//            @Override
//            public void error(Call call, IOException e) {
//
//            }
//
//            @Override
//            public void error(Call call, int code) {
//
//            }
//
//            @Override
//            public void suseff(Call call, Response response, Object o) {
//
//            }
//        }, BaseEntity.class);
    }

    private void bindAdapter() {

    }

    private void initView() {
        coupon_no_data_relativeLayout = (RelativeLayout) view.findViewById(R.id.coupon_no_data_relativeLayout);
        coupon_listView = (ListView) view.findViewById(R.id.coupon_listView);
        coupon_listView_data = new ArrayList<>();
        coupon_listView_Adapter = new CouponAdapter(getActivity(), coupon_listView_data, mParam1);
        coupon_listView.setAdapter(coupon_listView_Adapter);
    }


}
